import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Student implements Comparable<Student>{
    String name;
    int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int compareTo(Student o){
        return score-o.score;
    }

    public String toString() {
        return name+","+score;
    }
}

class NameCompare implements Comparator<Student>{
    public int compare(Student s1,Student s2){
        return s1.getName().compareTo(s2.getName());
    }
}

public class Main6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Student[] stus = new Student[n];
        for (int i = 0; i < stus.length; i++) {
            stus[i] = new Student(sc.next(), sc.nextInt());// 输入学生的姓名和成绩
        }
        Arrays.sort(stus);
        System.out.println("--按成绩排序--");
        for (int i = 0; i < stus.length; i++) {
            System.out.println(stus[i]);
        }
        System.out.println("--按姓名排序--");
        NameCompare nc = new NameCompare();
        Arrays.sort(stus, nc);

        for (int i = 0; i < stus.length; i++) {
            System.out.println(stus[i] );
        }
        sc.close();

    }
}

